package com.supersit.greensproject.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.supersit.greensproject.R;

/**
 * Created by cong on 2016/7/12.
 */
public class MyTitleBar extends FrameLayout{
    private OnClickListener ImgLeftlistener;
    private OnClickListener ImgRightlistener;
    private OnClickListener Titlelistener;
    private OnClickListener rightTextListener;
    private ImageView mImgLeft;
    private ImageView mImgRight;
    private TextView mTitle;
    private TextView rightText;
    private View view;

    public MyTitleBar(Context context,View view) {
        super(context);
        this.view = view;
        initview();
        OnClick();
    }

    private void OnClick() {
        mImgLeft.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (ImgLeftlistener != null) {
                    ImgLeftlistener.onClick(arg0);
                }
            }
        });
        mImgRight.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (ImgRightlistener != null) {
                    ImgRightlistener.onClick(arg0);
                }
            }
        });
        mTitle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (Titlelistener != null) {
                    Titlelistener.onClick(arg0);
                }
            }
        });
       rightText.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
               if(rightTextListener!=null){
                   rightTextListener.onClick(v);
               }
           }
       });

    }

    private void initview() {
        mImgLeft = (ImageView) view.findViewById(R.id.title_bar_left);
        mImgRight = (ImageView) view.findViewById(R.id.title_bar_right);
        mTitle = (TextView) view.findViewById(R.id.title_name);
        rightText = (TextView) view.findViewById(R.id.title_right_text);


    }
    /***
     * 设置子控件是否显示
     * @param isShow
     * @param v
     */
    public void setChildVisibility(boolean isShow, View v){
        if (isShow) {
            v.setVisibility(VISIBLE);
        }else{
            v.setVisibility(INVISIBLE);
        }
    }

    //设置监听事件
    public void setImg1Listener(OnClickListener listener){
        ImgLeftlistener = listener;
    }
    public void setImg2Listener(OnClickListener listener){
        ImgRightlistener = listener;
    }
    public void setTitleListener(OnClickListener listener){
        Titlelistener = listener;
    }
    public void setImgRightlistener(OnClickListener listener){
        rightTextListener = listener;
    }

    public ImageView getmImgLeft() {
        return mImgLeft;
    }

    public void setImgLeftByRes(int resId) {
        this.mImgLeft.setImageResource(resId);
    }

    public ImageView getmImgRight() {
        return mImgRight;
    }

    public void setImgRightByRes(int resId) {
        this.mImgRight.setImageResource(resId);
    }

    public TextView getmTitle() {
        return mTitle;
    }
    public void setTitleText(String text) {
        this.mTitle.setText(text);
    }
    public void showRightText(){
        rightText .setVisibility(View.VISIBLE);
        getmImgRight().setVisibility(View.GONE);
    }
    public void setRightTextText(String text){
        rightText.setText(text);
    }
}
